package com.example.remotecontrol.data.model

/**
 * 电视遥控器代码数据类
 * 定义了各个品牌电视的红外码
 */
data class TVRemoteCode(
    val brand: String,       // 品牌名称
    val model: String = "",  // 型号（可选）
    val address: Int,        // 设备地址码
    val commands: Map<TVRemoteKey, Int>  // 各功能键对应的命令码
)

/**
 * 电视遥控器按键枚举
 */
enum class TVRemoteKey {
    POWER,      // 电源
    VOLUME_UP,  // 音量+
    VOLUME_DOWN,// 音量-
    MUTE,       // 静音
    CHANNEL_UP, // 频道+
    CHANNEL_DOWN,// 频道-
    INPUT,      // 输入源
    HOME,       // 首页
    MENU,       // 菜单
    UP,         // 上导航
    DOWN,       // 下导航
    LEFT,       // 左导航
    RIGHT,      // 右导航
    OK,         // 确认
    BACK,       // 返回
    NUM_0,      // 数字0
    NUM_1,      // 数字1
    NUM_2,      // 数字2
    NUM_3,      // 数字3
    NUM_4,      // 数字4
    NUM_5,      // 数字5
    NUM_6,      // 数字6
    NUM_7,      // 数字7
    NUM_8,      // 数字8
    NUM_9       // 数字9
}

/**
 * 电视品牌枚举
 */
enum class TVBrand(val displayName: String) {
    SAMSUNG("三星"),
    LG("LG"),
    SONY("索尼"),
    SHARP("夏普"),
    PANASONIC("松下"),
    PHILIPS("飞利浦"),
    HISENSE("海信"),
    TCL("TCL"),
    SKYWORTH("创维"),
    CHANGHONG("长虹"),
    HAIER("海尔"),
    KONKA("康佳"),
    OTHER("其他")
}

/**
 * 预设的电视遥控代码数据库
 */
object TVRemoteCodeDatabase {
    // 三星电视代码
    val SAMSUNG = TVRemoteCode(
        brand = TVBrand.SAMSUNG.displayName,
        address = 0x0707,
        commands = mapOf(
            TVRemoteKey.POWER to 0x02,
            TVRemoteKey.VOLUME_UP to 0x07,
            TVRemoteKey.VOLUME_DOWN to 0x0B,
            TVRemoteKey.MUTE to 0x0F,
            TVRemoteKey.CHANNEL_UP to 0x12,
            TVRemoteKey.CHANNEL_DOWN to 0x10,
            TVRemoteKey.INPUT to 0x01,
            TVRemoteKey.MENU to 0x1A,
            TVRemoteKey.UP to 0x60,
            TVRemoteKey.DOWN to 0x61,
            TVRemoteKey.LEFT to 0x65,
            TVRemoteKey.RIGHT to 0x62,
            TVRemoteKey.OK to 0x68,
            TVRemoteKey.BACK to 0x58,
            TVRemoteKey.NUM_0 to 0x11,
            TVRemoteKey.NUM_1 to 0x04,
            TVRemoteKey.NUM_2 to 0x05,
            TVRemoteKey.NUM_3 to 0x06,
            TVRemoteKey.NUM_4 to 0x08,
            TVRemoteKey.NUM_5 to 0x09,
            TVRemoteKey.NUM_6 to 0x0A,
            TVRemoteKey.NUM_7 to 0x0C,
            TVRemoteKey.NUM_8 to 0x0D,
            TVRemoteKey.NUM_9 to 0x0E
        )
    )
    
    // LG电视代码
    val LG = TVRemoteCode(
        brand = TVBrand.LG.displayName,
        address = 0x04,
        commands = mapOf(
            TVRemoteKey.POWER to 0x08,
            TVRemoteKey.VOLUME_UP to 0x02,
            TVRemoteKey.VOLUME_DOWN to 0x03,
            TVRemoteKey.MUTE to 0x09,
            TVRemoteKey.CHANNEL_UP to 0x00,
            TVRemoteKey.CHANNEL_DOWN to 0x01,
            TVRemoteKey.INPUT to 0x0B,
            TVRemoteKey.MENU to 0x43,
            TVRemoteKey.UP to 0x40,
            TVRemoteKey.DOWN to 0x41,
            TVRemoteKey.LEFT to 0x07,
            TVRemoteKey.RIGHT to 0x06,
            TVRemoteKey.OK to 0x44,
            TVRemoteKey.BACK to 0x28,
            TVRemoteKey.NUM_0 to 0x10,
            TVRemoteKey.NUM_1 to 0x11,
            TVRemoteKey.NUM_2 to 0x12,
            TVRemoteKey.NUM_3 to 0x13,
            TVRemoteKey.NUM_4 to 0x14,
            TVRemoteKey.NUM_5 to 0x15,
            TVRemoteKey.NUM_6 to 0x16,
            TVRemoteKey.NUM_7 to 0x17,
            TVRemoteKey.NUM_8 to 0x18,
            TVRemoteKey.NUM_9 to 0x19
        )
    )
    
    // 海信电视代码
    val HISENSE = TVRemoteCode(
        brand = TVBrand.HISENSE.displayName,
        address = 0x00,
        commands = mapOf(
            TVRemoteKey.POWER to 0x12,
            TVRemoteKey.VOLUME_UP to 0x44,
            TVRemoteKey.VOLUME_DOWN to 0x43,
            TVRemoteKey.MUTE to 0x10,
            TVRemoteKey.CHANNEL_UP to 0x4A,
            TVRemoteKey.CHANNEL_DOWN to 0x4B,
            TVRemoteKey.INPUT to 0x38,
            TVRemoteKey.MENU to 0x41,
            TVRemoteKey.UP to 0x19,
            TVRemoteKey.DOWN to 0x1A,
            TVRemoteKey.LEFT to 0x1B,
            TVRemoteKey.RIGHT to 0x1C,
            TVRemoteKey.OK to 0x1D,
            TVRemoteKey.BACK to 0x16,
            TVRemoteKey.NUM_0 to 0x00,
            TVRemoteKey.NUM_1 to 0x01,
            TVRemoteKey.NUM_2 to 0x02,
            TVRemoteKey.NUM_3 to 0x03,
            TVRemoteKey.NUM_4 to 0x04,
            TVRemoteKey.NUM_5 to 0x05,
            TVRemoteKey.NUM_6 to 0x06,
            TVRemoteKey.NUM_7 to 0x07,
            TVRemoteKey.NUM_8 to 0x08,
            TVRemoteKey.NUM_9 to 0x09
        )
    )
    
    // 所有预设代码列表
    val allCodes = listOf(SAMSUNG, LG, HISENSE)
    
    // 根据品牌获取遥控代码
    fun getCodeByBrand(brand: TVBrand): TVRemoteCode? {
        return allCodes.find { it.brand == brand.displayName }
    }
}

/**
 * 电视遥控器红外码库
 * 存储不同品牌电视的红外控制码
 */
object TVRemoteCode {
    
    /**
     * 获取特定品牌的地址码
     */
    fun getAddressCode(brand: String): Int {
        return when (brand.lowercase()) {
            "samsung" -> 0x0707
            "lg" -> 0x2020
            "sony" -> 0x1010
            "tcl" -> 0x4040
            "hisense" -> 0x5050
            "philips" -> 0x0808
            "panasonic" -> 0x4004
            "sharp" -> 0x1414
            "toshiba" -> 0x0202
            "skyworth" -> 0x5151
            "haier" -> 0x4242
            "changhong" -> 0x5252
            "konka" -> 0x5353
            else -> 0x0707 // 默认使用三星的地址码
        }
    }
    
    /**
     * 获取常用命令的命令码映射
     */
    fun getCommandMap(brand: String): Map<String, Int> {
        return when (brand.lowercase()) {
            "samsung" -> samsungCommands
            "lg" -> lgCommands
            "sony" -> sonyCommands
            "tcl" -> tclCommands
            "hisense" -> hisenseCommands
            else -> samsungCommands // 默认使用三星的命令
        }
    }
    
    /**
     * 三星电视命令码
     */
    private val samsungCommands = mapOf(
        "POWER" to 0x02,
        "SOURCE" to 0x01,
        "MENU" to 0x1A,
        "UP" to 0x60,
        "DOWN" to 0x61,
        "LEFT" to 0x65,
        "RIGHT" to 0x62,
        "OK" to 0x68,
        "RETURN" to 0x58,
        "EXIT" to 0x2D,
        "VOLUME_UP" to 0x07,
        "VOLUME_DOWN" to 0x0B,
        "MUTE" to 0x0F,
        "CHANNEL_UP" to 0x12,
        "CHANNEL_DOWN" to 0x10,
        "NUMBER_0" to 0x11,
        "NUMBER_1" to 0x04,
        "NUMBER_2" to 0x05,
        "NUMBER_3" to 0x06,
        "NUMBER_4" to 0x08,
        "NUMBER_5" to 0x09,
        "NUMBER_6" to 0x0A,
        "NUMBER_7" to 0x0C,
        "NUMBER_8" to 0x0D,
        "NUMBER_9" to 0x0E,
        "HOME" to 0x79,
        "GUIDE" to 0x4F,
        "INFO" to 0x1F
    )
    
    /**
     * LG电视命令码
     */
    private val lgCommands = mapOf(
        "POWER" to 0x08,
        "SOURCE" to 0x0B,
        "MENU" to 0x43,
        "UP" to 0x40,
        "DOWN" to 0x41,
        "LEFT" to 0x07,
        "RIGHT" to 0x06,
        "OK" to 0x44,
        "RETURN" to 0x28,
        "EXIT" to 0x5B,
        "VOLUME_UP" to 0x02,
        "VOLUME_DOWN" to 0x03,
        "MUTE" to 0x09,
        "CHANNEL_UP" to 0x00,
        "CHANNEL_DOWN" to 0x01,
        "NUMBER_0" to 0x10,
        "NUMBER_1" to 0x11,
        "NUMBER_2" to 0x12,
        "NUMBER_3" to 0x13,
        "NUMBER_4" to 0x14,
        "NUMBER_5" to 0x15,
        "NUMBER_6" to 0x16,
        "NUMBER_7" to 0x17,
        "NUMBER_8" to 0x18,
        "NUMBER_9" to 0x19,
        "HOME" to 0x7C,
        "GUIDE" to 0xA9,
        "INFO" to 0xAA
    )
    
    /**
     * 索尼电视命令码
     */
    private val sonyCommands = mapOf(
        "POWER" to 0x15,
        "SOURCE" to 0x25,
        "MENU" to 0x60,
        "UP" to 0x74,
        "DOWN" to 0x75,
        "LEFT" to 0x34,
        "RIGHT" to 0x33,
        "OK" to 0x65,
        "RETURN" to 0x62,
        "EXIT" to 0x63,
        "VOLUME_UP" to 0x12,
        "VOLUME_DOWN" to 0x13,
        "MUTE" to 0x14,
        "CHANNEL_UP" to 0x10,
        "CHANNEL_DOWN" to 0x11,
        "NUMBER_0" to 0x20,
        "NUMBER_1" to 0x00,
        "NUMBER_2" to 0x01,
        "NUMBER_3" to 0x02,
        "NUMBER_4" to 0x03,
        "NUMBER_5" to 0x04,
        "NUMBER_6" to 0x05,
        "NUMBER_7" to 0x06,
        "NUMBER_8" to 0x07,
        "NUMBER_9" to 0x08,
        "HOME" to 0x6D,
        "GUIDE" to 0x65,
        "INFO" to 0x5C
    )
    
    /**
     * TCL电视命令码
     */
    private val tclCommands = mapOf(
        "POWER" to 0x40,
        "SOURCE" to 0x41,
        "MENU" to 0x5B,
        "UP" to 0x56,
        "DOWN" to 0x57,
        "LEFT" to 0x58,
        "RIGHT" to 0x59,
        "OK" to 0x5A,
        "RETURN" to 0x66,
        "EXIT" to 0x7D,
        "VOLUME_UP" to 0x42,
        "VOLUME_DOWN" to 0x43,
        "MUTE" to 0x50,
        "CHANNEL_UP" to 0x44,
        "CHANNEL_DOWN" to 0x45,
        "NUMBER_0" to 0x10,
        "NUMBER_1" to 0x11,
        "NUMBER_2" to 0x12,
        "NUMBER_3" to 0x13,
        "NUMBER_4" to 0x14,
        "NUMBER_5" to 0x15,
        "NUMBER_6" to 0x16,
        "NUMBER_7" to 0x17,
        "NUMBER_8" to 0x18,
        "NUMBER_9" to 0x19,
        "HOME" to 0x7C,
        "GUIDE" to 0x6C,
        "INFO" to 0x53
    )
    
    /**
     * 海信电视命令码
     */
    private val hisenseCommands = mapOf(
        "POWER" to 0x40,
        "SOURCE" to 0x12,
        "MENU" to 0x0D,
        "UP" to 0x19,
        "DOWN" to 0x1A,
        "LEFT" to 0x1B,
        "RIGHT" to 0x1C,
        "OK" to 0x1D,
        "RETURN" to 0x16,
        "EXIT" to 0x29,
        "VOLUME_UP" to 0x44,
        "VOLUME_DOWN" to 0x43,
        "MUTE" to 0x41,
        "CHANNEL_UP" to 0x45,
        "CHANNEL_DOWN" to 0x46,
        "NUMBER_0" to 0x00,
        "NUMBER_1" to 0x01,
        "NUMBER_2" to 0x02,
        "NUMBER_3" to 0x03,
        "NUMBER_4" to 0x04,
        "NUMBER_5" to 0x05,
        "NUMBER_6" to 0x06,
        "NUMBER_7" to 0x07,
        "NUMBER_8" to 0x08,
        "NUMBER_9" to 0x09,
        "HOME" to 0x67,
        "GUIDE" to 0x20,
        "INFO" to 0x13
    )
} 